//
//  NoteViewController.m
//  TWH-Asst3
//
//  Created by rlwilli2 on 3/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NoteViewController.h"
#import "ColorPickerView.h"

@implementation NoteViewController

@synthesize viewNote;
@synthesize textView;
@synthesize toolBar;

@synthesize nameField;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/




-(void)deletePressed{
	viewNote.deleteMe = YES;
	[self.navigationController popViewControllerAnimated:YES];
	[self.textView resignFirstResponder];
}

-(void)renamePressed{
	nameField.text = viewNote.noteTitle;
	[nameField becomeFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
	[textField resignFirstResponder];
	
	self.viewNote.noteTitle = textField.text;
	[self.navigationItem setTitle:viewNote.noteTitle];
	
	return NO;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
	self.viewNote.noteTitle = [textField.text stringByReplacingCharactersInRange:range withString:string];
	[self.navigationItem setTitle:viewNote.noteTitle];
	
	return YES;
}

-(void)colorCallback{
	NSLog(@"ccb");
	
	self.viewNote.noteColor = cpv.selectedColor;
	
}

-(void)colorPressed{

	
	cpv.selectedColor = viewNote.noteColor;

	cpv.callobj = self;
	
	[self.navigationController pushViewController:cpv animated:YES];
}

-(void)kbPressed{
	
	if([self.textView isFirstResponder])
		[self.textView resignFirstResponder];
	else
		[self.textView becomeFirstResponder];
	
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	cpv = [[ColorPickerView alloc]initWithNibName:@"ColorPickerView" bundle:nil];
	
	UIBarButtonItem *donebtn = [[UIBarButtonItem alloc] initWithTitle:@"Keyboard" style:UIBarButtonItemStyleDone  target:self action:@selector(kbPressed)];
	[donebtn autorelease];
	
	//self.navigationItem.leftBarButtonItem = self.editButtonItem;
	self.navigationItem.rightBarButtonItem = donebtn;

	
	UIBarButtonItem *labelbtn = [[UIBarButtonItem alloc] initWithTitle:@"Note:" style:UIBarButtonItemStylePlain target:self action:nil];
	
	UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
	
	UIBarButtonItem *delbtn = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(deletePressed)];
	UIBarButtonItem *renbtn = [[UIBarButtonItem alloc] initWithTitle:@"Rename" style:UIBarButtonItemStyleBordered target:self action:@selector(renamePressed)];
	UIBarButtonItem *colbtn = [[UIBarButtonItem alloc] initWithTitle:@"Color" style:UIBarButtonItemStyleBordered target:self action:@selector(colorPressed)];
	
	
	self.toolbarItems = [NSArray arrayWithObjects:labelbtn,spacer,delbtn,renbtn,colbtn,nil];
	

}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(void)viewWillAppear:(BOOL)animated{
	
	[super viewWillAppear:animated];
	
	[self.navigationItem setTitle:viewNote.noteTitle];
	self.textView.text = self.viewNote.noteBody;
}

-(void)viewWillDisappear:(BOOL)animated{
	
	self.viewNote.noteBody = self.textView.text;
	[self.textView resignFirstResponder];
}

@end
